/** EDITOR VIEW*/

/** CONSTRUCTOR */
function Editor(controller){
    this.controller = controller;
}

/** EVENT LISTENERS */
Editor.prototype.ListenForEvents = function(){

    var controller = this.controller;
    var view = this;

    /* Listen for clicks on the whole document */
    $(document).click(function(event){
        if(!$(event.target).hasClass('component')){
            $('#editor').remove();
            controller.ShowEditorPanel(event.pageX, event.pageY);
            controller.targetElement = event.target.id;

            /* Highlight the clicked element */
            view.HightlightClickedElement(controller.targetElement);
        }

        /* Handle click on addRule element of evo editor */
        else if($(event.target).attr('id') == 'addRule'){
            $('#propertyEditor').slideToggle('slow');
        }

        /* Handle click on an a rule in the 'ADD RULE' tab to add it to the current rules */
        else if($(event.target).hasClass('propertyRule')){

            /* Get the name and id */
            var propertyName = $(event.target).html();
            var propertyId = $(event.target).attr('id');

            /* Ask the controller to generate the required tab */
            controller.AddPropertyEditor(propertyName.toLowerCase(), propertyId);

            /* Remove the clicked element */
            $(event.target).remove().slideUp();
        }

        /* Handle click on a rule, show it's editing pane */
        else if($(event.target).hasClass('editorElement')){
            $('.edit#' + $(event.target).attr('id')).slideToggle('slow');
        }

        /* Handle the close command, remove the editor */
        if($(event.target).attr('id') == 'close'){
            controller.ToggleEditorPanel(0.0,true);
        }
    });
}

/** Highlights the clicked function */
Editor.prototype.HightlightClickedElement = function(target){
    $('#'+target).css('border', '2px dotted red');
}

